package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC228 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> summaryRanges(int[] arr) {
            List<String> res = new ArrayList<>();
            if (null == arr || arr.length <= 0) return res;

            int step = 1;
            for (int i = 0; i < arr.length; ) {
                step = 1;
                while (i + step < arr.length && arr[i + step] - arr[i] == step) step *= 2;
                step /= 2;
                if (step == 0) res.add(String.valueOf(arr[i++]));
                else {
                    int left = step, right = i + step * 2 > arr.length ? arr.length - 1 - i : step * 2, mid = 0;
                    while (left <= right) {
                        mid = left + (right - left) / 2;
                        if (arr[i + mid] - arr[i] == mid) break;
                        if (arr[i + mid] - arr[i] > mid) right = mid - 1;
                        else left = mid + 1;
                    }

                    res.add(arr[i] + "->" + arr[i + mid]);
                    i += mid + 1;
                }
            }

            return res;
        }
    }


    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[0,2,3,4]");
        System.out.println(solution.summaryRanges(arr));
    }

    public static void main(String[] args) throws Exception {
        LC228 an = new LC228();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
